#include "password.h"

Password::Password(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Init();
    connect_Init();
}
void Password::ui_Init()
{
    for(int i=0;i<3;i++)//创建主屏幕显示的按键
    {
         theLabel[i] = new QLabel(this);
    }
    theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
    theLabel[0]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[0]->setText("O3设置");

    for(int i=0;i<4;i++)//创建主屏幕显示的按键
    {
         theBtn[i] = new QPushButton(this);
//        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
        theBtn[i]->setFixedSize(120,90);
btnlist_ui.append(theBtn[i]);

}
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(170, 150, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/O3/传递标准.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(170, 150, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/O3/传递标准2.png); /* 背景图片 */"+"}");
theBtn[2]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(230, 210, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/O3/光度计控制.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(230, 210, 155);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/O3/光度计控制2.png); /* 背景图片 */"+"}");
theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(220, 50, 105);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/O3/气相滴定.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(220, 50, 105);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/O3/气相滴定2.png); /* 背景图片 */"+"}");
theBtn[3]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+"color: rgb(255,125,25);	  /* 文本颜色 */"+"background-color: rgb(60, 255, 152);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+"background-color: rgb(60, 255, 152);	    /* 背景颜色 */ ;"+"color: rgb(255,125,25);	  /* 文本颜色 */"+ "background-image: url(:/pic/Airflow_mode/返回2.png); /* 背景图片 */"+"}");

btnlist_ui.at(0)->setText(tr("设置密码"));
btnlist_ui.at(1)->setText(tr("锁定机器"));
btnlist_ui.at(2)->setText(tr("移除密码"));
btnlist_ui.at(3)->setText(tr("返回"));

QGridLayout *theLayout = new QGridLayout();
theLayout->addWidget(btnlist_ui.at(0), 1, 0);
theLayout->addWidget(btnlist_ui.at(1), 1, 1);
theLayout->addWidget(btnlist_ui.at(2), 1, 2);
theLayout->addWidget(btnlist_ui.at(3), 1, 3);
//theLayout->setVerticalSpacing(50);
//theLayout->setHorizontalSpacing(10);
theLayout->setContentsMargins(0,120,0,0);//(int left, int top, int right, int bottom)
//theLayout->setColumnStretch(1,22);
theLayout->setSizeConstraint(QLayout::SetMinimumSize);
  setLayout(theLayout);

  QWidget* widget = new QWidget(this);
  widget->setGeometry(0,0,640,330);
  widget->setLayout(theLayout);

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}
void Password::connect_Init()
{
    //设置密码
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        Password_Set *menux = new Password_Set(this);
        menux->show();

    });

    //锁定机器
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){

        {
            keyPad *board = new keyPad(this);
            board->show();
            connect(board,&keyPad::changename2,this,&Password::Recive_Gas_SelData);
            connect(board,&keyPad::destroyed,[=](){
                btnlist_ui.at(1)->setFocus();
            });
        }
        if(!changepw_mode){
            the_Waring_Tip=("请先输入密码！");
            waring_Tip *menux = new waring_Tip(this);
            menux->show();

        }

    });
    //恢复默认值
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){
        the_Waring_Tip=("已恢复默认密码");
        waring_Tip *menux = new waring_Tip(this);
        menux->show();
        qstrcpy(Sysset->password,"000000");

    });
    //返回
    connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });
}

void Password::Recive_Gas_SelData(QString s)
{
        if(Sysset->password  == s)
        {
            lock_instrument *menux = new lock_instrument(this);
            menux->show();
        }
        else {
                the_Waring_Tip=("密码输入错误！");
                waring_Tip *menux = new waring_Tip(this);
                menux->show();
        }


}
bool Password::eventFilter(QObject *watched, QEvent *event)
{

//            key_Event::key_Event1(pos,watched, event, btnlist, btnlist_ui);
        for(int i=0;i<btnlist_ui.length();i++)
            if(btnlist_ui.at(i) == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
               QKeyEvent *ev = (QKeyEvent *)event;
               QPushButton *button = (QPushButton *)watched;
               switch (ev->key())
               {
               case OK:
                 {
                   emit  button->clicked();
                   break;
                 }
               case UP://上
               {
                   if(focusWidget() != btnlist_ui.first())
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   focusPreviousChild();
                   break;
               }
               case DOWN://下
               {
                   if(focusWidget() != btnlist_ui.back())
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   focusNextChild();
                   break;
               }
               case LEFT://左
               {
                 if(focusWidget() != btnlist_ui.first())
                      focusPreviousChild();
                 break;
               }
               case RIGHT://右
               {
                 if(focusWidget() != btnlist_ui.back())
                      focusNextChild();
              break;
               }
               case INIT: //打开初始化界面
               {
                   init_Show *show = new init_Show(this);
                   show->show();
                   this->hide();
                   break;
               }
               default:
                   break;
               }
            }
            else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
                      {
                          QPushButton *focus_button = (QPushButton *)watched;
                                  focus_button->setFixedSize(160,120);
//                                  focus_button->setStyleSheet(focusBtn[2]->styleSheet());
//                                  focus_button->setIconSize(QSize(1,2));
                      }
            else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
                     {
                QPushButton *focus_button = (QPushButton *)watched;
                        focus_button->setFixedSize(120,90);
                     }
            }
}
